package com.hxx.mybatisDemo;

import com.hxx.domain.IdCard;
import com.hxx.domain.Orders;
import com.hxx.domain.Person;
import com.hxx.domain.Users;
import com.hxx.mapper.IdCardMapper;
import com.hxx.mapper.OrdersMapper;
import com.hxx.mapper.PersonMapper;
import com.hxx.mapper.UsersMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class TestDemo1 {
    SqlSession sqlSession;
    @Before
    public void init(){
        System.out.println("-------Before--------");
        try {
            //读取Mybatis配置文件
            InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
            //基于读取Mybatis配置文件的输入流创建SqlSessionFactory对象
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);
            //获取SqlSession对象
            sqlSession = build.openSession();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Test
    public void test1() {//one-to-one查询
        Person person = sqlSession.getMapper(PersonMapper.class)
                .show(1);
        System.out.println(person);
    }
    @Test
    public void test2() {//one-to-one关联查询
        Person person = sqlSession.getMapper(PersonMapper.class)
                .show1(1);
        System.out.println(person);
    }
    @Test
    public void test3() {//one-to-one保存
        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
        IdCardMapper idCardMapper = sqlSession.getMapper(IdCardMapper.class);
        Person person = new Person();
        person.setAge(20).setName("张三").setSex("男");
        personMapper.insertPerson(person);

        //使用person返回的id作为idCard的id
        IdCard idCard = new IdCard();
        idCard.setId(person.getId()).setCode("456987");
        idCardMapper.insertCard(idCard);
    }

    @Test
    public void test4() {//one-to-many查询
        List<Users> allUsers = sqlSession.getMapper(UsersMapper.class).findAllUsers(1);
        allUsers.forEach(System.out::println);
    }
    @Test
    public void test5() {//one-to-many保存
        UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
        OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class);
        //保存
        Users users = new Users().setUsername("张三").setAddress("北京");
        usersMapper.insertUser(users);

        Orders orders1 = new Orders().setNumber("1234").setUserId(users.getId());
        Orders orders2 = new Orders().setNumber("4567").setUserId(users.getId());
        //保存
        ordersMapper.insertOrder(orders1);
        ordersMapper.insertOrder(orders2);
    }

    @After
    public void close() {
        System.out.println("--------After-------");
        sqlSession.commit();
        sqlSession.close();
    }
}
























